﻿Public Class danhsachKH
    Private KhachHangBS As KhachHangBS
    Private KhachHangGD As KhachHangGD
    Private cm As CurrencyManager


    '------- PageLoad

    Private Sub danhsachKH_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDGV_KhachHang()
    End Sub


    '-------- Load
    Private Sub LoadDGV_KhachHang()
        dgrvDSKH.ClearSelection()
        Dim KhachHangList As New List(Of KhachHangGD)
        KhachHangBS = New KhachHangBS
        KhachHangList = KhachHangBS.GetData()
        dgrvDSKH.DataSource = KhachHangList
    End Sub

    Private Sub ClearTextBox()
        txtmakh.Text = ""
        txttenkh.Text = ""
        txtdienthoaikh.Text = ""
        txtdiachikh.Text = ""
    End Sub

    Private Sub ClearBinding()
        txtmakh.DataBindings.Clear()
        txttenkh.DataBindings.Clear()
        txtdienthoaikh.DataBindings.Clear()
        txtdiachikh.DataBindings.Clear()
    End Sub


    '------------ Các Button

    Private Sub btnthemkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthemkh.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btncapnhatkh.Visible = False
        ClearTextBox()
        ClearBinding()

    End Sub

    Private Sub btnsuakh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsuakh.Click
        Me.GroupBox1.Visible = False
        Me.GroupBox2.Visible = True
        btnaddkh.Visible = False
        txtmakh.Enabled = False

        Dim maKhachHang As String
        maKhachHang = dgrvDSKH.SelectedRows.Item(0).Cells(1).Value
        Dim hoTen As String
        hoTen = dgrvDSKH.SelectedRows.Item(0).Cells(2).Value
        Dim dienThoai As String
        dienThoai = dgrvDSKH.SelectedRows.Item(0).Cells(3).Value
        Dim diaChi As String
        diaChi = dgrvDSKH.SelectedRows.Item(0).Cells(4).Value

        txtmakh.Text = maKhachHang
        txttenkh.Text = hoTen
        txtdienthoaikh.Text = dienThoai
        txtdiachikh.Text = diaChi
    End Sub

    Private Sub btnxoakh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnxoakh.Click
        Dim maKhachHang As String
        maKhachHang = dgrvDSKH.SelectedRows.Item(0).Cells(0).Value
        Dim hoTen As String
        hoTen = dgrvDSKH.SelectedRows.Item(0).Cells(1).Value
        Dim dienThoai As String
        dienThoai = dgrvDSKH.SelectedRows.Item(0).Cells(2).Value
        Dim diaChi As String
        diaChi = dgrvDSKH.SelectedRows.Item(0).Cells(3).Value
        KhachHangGD = New KhachHangGD(maKhachHang, hoTen, dienThoai, diaChi)
        KhachHangBS.Delete(KhachHangGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_KhachHang()
    End Sub

    Private Sub btndongkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndongkh.Click
        Me.Close()
    End Sub

    Private Sub btnbackkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnbackkh.Click
        ClearBinding()
        LoadDGV_KhachHang()
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        btncapnhatkh.Visible = True
        btnaddkh.Visible = True
    End Sub

   
    Private Sub btnaddkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddkh.Click
        Dim maKhachHang As String = txtmakh.Text
        Dim hoTen As String = txttenkh.Text
        Dim dienThoai As String = txtdienthoaikh.Text
        Dim diaChi As String = txtdiachikh.Text
        KhachHangGD = New KhachHangGD(maKhachHang, hoTen, dienThoai, diaChi)
        KhachHangBS.Insert(KhachHangGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_KhachHang()
        btncapnhatkh.Visible = True
        btnaddkh.Visible = True
    End Sub

    Private Sub btncapnhatkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncapnhatkh.Click
        Dim maKhachHang As String = txtmakh.Text
        Dim hoTen As String = txttenkh.Text
        Dim dienThoai As String = txtdienthoaikh.Text
        Dim diaChi As String = txtdiachikh.Text
        KhachHangGD = New KhachHangGD(maKhachHang, hoTen, dienThoai, diaChi)
        KhachHangBS.Update(KhachHangGD)
        Me.GroupBox1.Visible = True
        Me.GroupBox2.Visible = False
        LoadDGV_KhachHang()
        btncapnhatkh.Visible = True
        btnaddkh.Visible = True
    End Sub

    Private Sub btntimkh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntimkh.Click
        dgrvDSKH.DataSource = KhachHangBS.GetByName(txttimkh.Text)
    End Sub

    Private Sub RadioButton1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RadioButton1.MouseClick
        LoadDGV_KhachHang()
    End Sub

    Private Sub radiotimkh_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles radiotimkh.MouseClick
        txttimkh.Focus()
    End Sub

    Private Sub txttimkh_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txttimkh.MouseClick
        radiotimkh.Checked = True
    End Sub
End Class